package net.wanho.service.impl;

import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.dto.BuyListAddDTO;
import net.wanho.dto.BuyListDTO;
import net.wanho.dto.BuyListInStoreDTO;
import net.wanho.dto.BuyListQueryDTO;
import net.wanho.dto.BuyListUpdateDTO;
import net.wanho.mapper.BuyListMapper;
import net.wanho.po.BuyList;
import net.wanho.po.InStore;
import net.wanho.service.BuyListService;
import net.wanho.service.InStoreService;
import net.wanho.util.CurrentUtil;
import net.wanho.vo.BuyListExportVO;
import net.wanho.vo.BuyListVo;
import net.wanho.vo.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 32033
 * @description 针对表【buy_list(采购单表)】的数据库操作Service实现
 * @createDate 2025-01-11 17:01:23
 */
@Service
@RequiredArgsConstructor
public class BuyListServiceImpl extends ServiceImpl<BuyListMapper, BuyList>
        implements BuyListService {

    private final InStoreService inStoreService;
    private final ProductServiceImpl productService;

    @Override
    public PageInfo<BuyListVo> purchasePageList(int pageNum, int pageSize, BuyListDTO buyListDTO) {
        Page<BuyListVo> page = new Page<>(pageNum, pageSize);
//        LambdaQueryWrapper<BuyListVo> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(!ObjectUtils.isEmpty(buyListDTO.getStoreId()), BuyListVo::getStoreId, buyListDTO.getStoreId());
//        wrapper.like(!ObjectUtils.isEmpty(buyListDTO.getProductName()), BuyListVo::getProductName, buyListDTO.getProductName());
//        wrapper.like(!ObjectUtils.isEmpty(buyListDTO.getBuyUser()), BuyListVo::getBuyUser, buyListDTO.getBuyUser());
//        wrapper.like(!ObjectUtils.isEmpty(buyListDTO.getIsIn()), BuyListVo::getIsIn, buyListDTO.getIsIn());
//        wrapper.ge(!ObjectUtils.isEmpty(buyListDTO.getStartTime()), BuyListVo::getBuyTime, buyListDTO.getStartTime());
//        wrapper.le(!ObjectUtils.isEmpty(buyListDTO.getEndTime()), BuyListVo::getBuyTime, buyListDTO.getEndTime());
        QueryWrapper<BuyListVo> wrapper = new QueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(buyListDTO.getStoreId()), "bl.store_id", buyListDTO.getStoreId());
        wrapper.like(!ObjectUtils.isEmpty(buyListDTO.getProductName()), "p.product_name", buyListDTO.getProductName());
        wrapper.like(!ObjectUtils.isEmpty(buyListDTO.getBuyUser()), "bl.buy_user", buyListDTO.getBuyUser());
        wrapper.like(!ObjectUtils.isEmpty(buyListDTO.getIsIn()), "bl.is_in", buyListDTO.getIsIn());
        wrapper.ge(!ObjectUtils.isEmpty(buyListDTO.getStartTime()), "bl.buy_time", buyListDTO.getStartTime());
        wrapper.le(!ObjectUtils.isEmpty(buyListDTO.getEndTime()), "bl.buy_time", buyListDTO.getEndTime());

//        this.baseMapper.purchasePageList(page, wrapper);
        this.baseMapper.purchasePageList(page, wrapper);

        PageInfo<BuyListVo> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setPageCount(page.getCurrent());
        pageInfo.setResultList(page.getRecords());
        pageInfo.setLimitIndex(page.offset());
        return pageInfo;

    }

    @Override
    public void saveBuyList(BuyListAddDTO buyListAddDTO) {
        BuyList buyList = new BuyList();
        buyList.setProductId(buyListAddDTO.getProductId());
        buyList.setStoreId(buyListAddDTO.getStoreId());
        buyList.setBuyNum(buyListAddDTO.getBuyNum());
        buyList.setFactBuyNum(0);// 实际采购数量待入库时填写
        buyList.setBuyTime(new Date());// 使用当前时间
        buyList.setSupplyId(buyListAddDTO.getSupplyId());
        buyList.setPlaceId(buyListAddDTO.getPlaceId());
        buyList.setBuyUser(buyListAddDTO.getBuyUser());
        buyList.setPhone(buyListAddDTO.getPhone());
        buyList.setIsIn("0");// 设置未入库状态
        this.save(buyList);
    }

    @Override
    public void updateBuyList(BuyListUpdateDTO updateDTO) {
        BuyList buyList = new BuyList();
        buyList.setBuyId(updateDTO.getBuyId());
        buyList.setFactBuyNum(updateDTO.getFactBuyNum());
        buyList.setBuyUser(updateDTO.getBuyUser());
        buyList.setPhone(updateDTO.getPhone());
        buyList.setBuyTime(new Date());
        this.updateById(buyList);
    }

    @Override
    public void deleteBuyList(Integer id) {
        // 判断参数是否为空
        Assert.notNull(id, "采购单ID不能为空！");

        // 判断采购单是否存在
        BuyList existBuyList = this.getById(id);
        Assert.notNull(existBuyList, "采购单不存在！");

        // 入库状态判断
        Assert.isTrue(!"1".equals(existBuyList.getIsIn()), "采购单已入库，不能删除！");

        // 删除
        this.removeById(id);
    }

    @Override
    public void saveInWarehouseRecord(BuyListInStoreDTO buyListInStoreDTO) {
        BuyList updateBuyList = new BuyList();
        updateBuyList.setBuyId(buyListInStoreDTO.getBuyId());
        updateBuyList.setFactBuyNum(buyListInStoreDTO.getFactBuyNum());
        updateBuyList.setIsIn("1");// 设置入库状态
        updateBuyList.setBuyTime(DateUtils.parseDate(buyListInStoreDTO.getBuyTime())); // 更新采购时间
        updateBuyList.setBuyUser(buyListInStoreDTO.getBuyUser()); // 更新采购人
        updateBuyList.setPhone(buyListInStoreDTO.getPhone()); // 更新电话
        updateBuyList.setSupplyId(buyListInStoreDTO.getSupplyId()); // 更新供应商
        updateBuyList.setPlaceId(buyListInStoreDTO.getPlaceId()); // 更新产地
        this.updateById(updateBuyList);

//        Product product = productService.getById(buyListInStoreDTO.getProductId());
//        product.setProductInvent(product.getProductInvent() + buyListInStoreDTO.getBuyNum());
//        productService.updateById(product);

        InStore inStore = new InStore();
        inStore.setStoreId(buyListInStoreDTO.getStoreId());
        inStore.setProductId(buyListInStoreDTO.getProductId());
        inStore.setInNum(buyListInStoreDTO.getFactBuyNum());
        inStore.setCreateBy(CurrentUtil.getId());
        inStore.setCreateTime(new Date());
        inStore.setIsIn("0"); // 设置入库状态为未入库
        inStoreService.save(inStore);
    }

    @Override
    public List<BuyListExportVO> exportTable(BuyListQueryDTO queryDTO) {
        QueryWrapper<BuyList> wrapper = new QueryWrapper<>();
        wrapper.eq(queryDTO.getStoreId() != null, "bl.store_id", queryDTO.getStoreId())
                .like(StringUtils.hasText(queryDTO.getBuyUser()), "bl.buy_user", queryDTO.getBuyUser())
                .eq(StringUtils.hasText(queryDTO.getIsIn()), "bl.is_in", queryDTO.getIsIn())
                .ge(StringUtils.hasText(queryDTO.getStartTime()), "bl.buy_time", queryDTO.getStartTime())
                .le(StringUtils.hasText(queryDTO.getEndTime()), "bl.buy_time", queryDTO.getEndTime())
                .orderByDesc("bl.buy_time");

        // 2. 计算分页参数
        Integer limitIndex = (queryDTO.getPageNum() - 1) * queryDTO.getPageSize();

        // 3. 执行分页查询
        return baseMapper.selectExportList(wrapper, queryDTO.getProductName(), limitIndex, queryDTO.getPageSize());
    }


}




